package com.why.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.dao.NewsMapper;
import com.why.pojo.News;
import com.why.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsMapper newsMapper;

    @Override
    public List<News> newsList() {
        return newsMapper.newsList();
    }

    @Override
    public PageInfo<News> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> list = newsMapper.selectAllList();
        PageInfo<News> newsPageInfo = new PageInfo<>(list);
        return newsPageInfo;
    }

    @Override
    public News selectByPrimaryKey(Integer id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteByPrimaryKey(Integer id) {
        if (newsMapper.deleteByPrimaryKey(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertSelective(News record) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        record.setCreatetime(format.format(new Date()));
        if (newsMapper.insertSelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateByPrimaryKeySelective(News record) {
        if (newsMapper.updateByPrimaryKeySelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public News selectNewsByTitle(String title) {
        return newsMapper.selectNewsByTitle(title);
    }
}
